package ex.recruit.action;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modules.recruit.entity.MatReview;
import modules.recruit.entity.Region;
import modules.recruit.service.MatReviewService;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.guinea.core.PropertyFilterForSql;
import com.guinea.core.orm.Page;
import com.guinea.core.orm.PropertyFilter;
import com.guinea.core.service.EntityService;
import com.guinea.core.utils.ServletUtils;
import com.guinea.core.utils.StringUtils;

import ex.recruit.form.MatReviewFormEx;
import ex.recruit.service.MatItemServiceEx;
import ex.system.service.AttachServiceEx;

public class MatReviewActionEx extends CommonApplyJudgment {
	
	MatItemServiceEx matItemServiceEx;
	MatReviewService matReviewService;
	AttachServiceEx attachServiceEx;

	public EntityService getEntityService() {
		return matReviewService;
	}

	public MatReviewService getMatReviewService() {
		return matReviewService;
	}

	public void setMatReviewService(MatReviewService matReviewService) {
		this.matReviewService = matReviewService;
	}


	public MatItemServiceEx getMatItemServiceEx() {
		return matItemServiceEx;
	}

	public void setMatItemServiceEx(MatItemServiceEx itemServiceEx) {
		matItemServiceEx = itemServiceEx;
	}

	public AttachServiceEx getAttachServiceEx() {
		return attachServiceEx;
	}

	public void setAttachServiceEx(AttachServiceEx attachServiceEx) {
		this.attachServiceEx = attachServiceEx;
	}

	/**
	 * 初始化业务对象的函数.
	 */
	protected void findEntity(ActionForm form, HttpServletRequest request) {
		super.findEntity(form, request);
		if (entity == null && applyBase != null) {
			entity = applyBase.getMaterialReview();
		}
	}
	
	/**
	 * 列出所有对象的Action函数.
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 从request中获取含属性前缀名的参数,构造去除前缀名后的参数Map.
		Map filterParamMap = ServletUtils.getParametersStartingWith(request,
				FILTER_PREFIX + "_");
		List filters = PropertyFilterForSql.buildFromParaMap(filterParamMap);
		Page page = PropertyFilter.buildPageFromHttpRequest(request,
				REQUEST_PAGE);
		
		filters.add(new PropertyFilterForSql("step", "5"));
		page = getApplyBaseServiceEx().getApplyProcess(page, filters, true);

		request.setAttribute(REQUEST_PAGE, page);
		request.setAttribute(FILTER_MAP, filterParamMap);
		return mapping.findForward(LIST);
	}
	
	/**
	 * 显示修改对象Form的Action函数.
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		bindForm(form, entity);
		MatReviewFormEx matReviewFormEx = (MatReviewFormEx)form;
		matReviewFormEx.setApplyBaseId(applyBaseId);
		getMatItemServiceEx().bindReviewForm(matReviewFormEx);
		//建设地址
		if(StringUtils.isBlank(matReviewFormEx.getBuiltAddress())){
			Region r = null;
			Set regions = applyBase.getRegion();
			for (Iterator iter = regions.iterator(); iter.hasNext();) {
				Region region = (Region) iter.next();
				if("1".equals(region.getStatus()) && "1".equals(region.getSerial())){
					r = region;
					break;
				}
			}
			if(r != null){
				matReviewFormEx.setBuiltAddress(r.getAddress());
			}
		}
		
		return handleEdit(mapping,form,request,response);
	}
	
	/**
	 * 保存前的验证.
	 */
	public ActionForward saveValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (isCancelled(request)){
			valid = false;
			return mapping.findForward(LIST);
		}
		if (!validateToken(request))
			return handleToken(mapping, form, request, response);

		initFirst(form, request);
		if (!validateApplyBase())
			return handleEntityMissing(mapping, form, request, response);

		MatReviewFormEx matReviewFormEx = (MatReviewFormEx)form;
		FormFile f = matReviewFormEx.getFile();
		if (f != null && f.getFileSize() > 2 * 1024 * 1024) {
			valid = false;
			saveDirectlyError(request,"请上传一个大小不超过2M的附件。");
			return handleEdit(mapping,form,request,response);
		}
		return null;
	}
	
	protected void doInitEntity(HttpServletRequest request, ActionForm form) {
		super.doInitEntity(request, form);
		MatReviewFormEx matReviewFormEx = (MatReviewFormEx)form;
		getMatItemServiceEx().saveItemFormEx(matReviewFormEx);
		//附件
		FormFile f = matReviewFormEx.getFile();
		if(f!= null){
			Long attachId = getAttachServiceEx().saveFile(f);
			matReviewFormEx.setAttachId(attachId);
			MatReview matReview = (MatReview)entity;
			matReview.setAttachId(attachId);
		}
		
		if (isCreateOperation) {
			MatReview matReview = (MatReview) entity;
			matReview.setBase(applyBase);
			entity = matReview;
		}
	}

	/**
	 * view验证.
	 */
	public ActionForward viewValidator(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		initFirst(form, request);

		return null;
	}
	
	/**
	 * 查看业务对象（不能修改）的Action函数.
	 */
	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if(entity == null){
			 entity = new MatReview();
			 request.setAttribute("applyBase",applyBase);
		}
		
		request.setAttribute(REQUEST_ENTITY, entity);
		
		bindForm(form, entity);
		MatReviewFormEx matReviewFormEx = (MatReviewFormEx)form;
		matReviewFormEx.setApplyBaseId(applyBaseId);
		getMatItemServiceEx().bindReviewForm(matReviewFormEx);
		request.setAttribute("matReviewVo",matReviewFormEx);
		
		return mapping.findForward(VIEW);
	}

}
